#
#.SYNOPSIS
#	createQMLBank.ps1 -i <inputfile> [-o <outputfile>] [-startID <number>] [-prefix <description prefix>]
#	Author: Roel Bindels
#
#.DESCRIPTION
#    
#
#	Tested on: PowerShell ISE V2.
#

param(
    [string]$inputFile,
    [string]$outputFile = "$pwd\$inputFile.qml",
    [string]$prefix,
    [int]$startId
)


. .\QMLWriter.ps1

$gCounter

#
#.SYNOPSIS
#
#
#.DESCRIPTION
#    
#
#----------------------------------------
function createQMLOutcome{
    param($doc,$parent, $a_strId, $a_strCondition, $a_strContent, $a_Answers)	        

    $nCondition = [int][char]$a_strCondition - 65
    $nScore = 0
    $i = 0
    
    if($a_Answers."choice-a"){
        if($i -eq $nCondition){
    	   $nScore = 1
        }
        else{
           $nScore = 0 
        }        
        
        $test = [string]$i
        $test2 = '"' + [string]$i + '"'
                
		$Outcome = createQMLOutcomeType  -doc $doc `
                -parent $parent `
                -a_strId $test `
                -a_nScore $nScore
	
	    createQMLConditionType  -doc $doc `
                -parent $Outcome `
                -a_strCondition $test2  
  	                       	
	    createQMLContentType  -doc $doc `
                -parent $Outcome `
                -a_strContent $a_strContent  
        $i++
    }
        if($a_Answers."choice-b"){
        if($i -eq $nCondition){
    	   $nScore = 1
        }
        else{
           $nScore = 0 
        }
        
        $test = [string]$i
        $test2 = '"' + [string]$i + '"'
        
		$Outcome = createQMLOutcomeType  -doc $doc `
                -parent $parent `
                -a_strId $test `
                -a_nScore $nScore
	
	    createQMLConditionType  -doc $doc `
                -parent $Outcome `
                -a_strCondition $test2 
  	                       	
	    createQMLContentType  -doc $doc `
                -parent $Outcome `
                -a_strContent $a_strContent  
        $i++
    }
    if($a_Answers."choice-c"){
        if($i -eq $nCondition){
    	   $nScore = 1
        }
        else{
           $nScore = 0 
        }
        
        $test = [string]$i
        $test2 = '"' + [string]$i + '"'
        
		$Outcome = createQMLOutcomeType  -doc $doc `
                -parent $parent `
                -a_strId $test `
                -a_nScore $nScore
	
	    createQMLConditionType  -doc $doc `
                -parent $Outcome `
                -a_strCondition $test2 
  	                       	
	    createQMLContentType  -doc $doc `
                -parent $Outcome `
                -a_strContent $a_strContent  
        $i++
    }
    if($a_Answers."choice-d"){
        if($i -eq $nCondition){
    	   $nScore = 1
        }
        else{
           $nScore = 0 
        }
        
        $test = [string]$i
        $test2 = '"' + [string]$i + '"'
        
		$Outcome = createQMLOutcomeType  -doc $doc `
                -parent $parent `
                -a_strId $test `
                -a_nScore $nScore
	
	    createQMLConditionType  -doc $doc `
                -parent $Outcome `
                -a_strCondition $test2  
  	                       	
	    createQMLContentType  -doc $doc `
                -parent $Outcome `
                -a_strContent $a_strContent  
        $i++
    }
    if($a_Answers."choice-e"){
        if($i -eq $nCondition){
    	   $nScore = 1
        }
        else{
           $nScore = 0 
        }
        
        $test = [string]$i
        $test2 = '"' + [string]$i + '"'
        
		$Outcome = createQMLOutcomeType  -doc $doc `
                -parent $parent `
                -a_strId $test `
                -a_nScore $nScore
	
	    createQMLConditionType  -doc $doc `
                -parent $Outcome `
                -a_strCondition $test2
  	                       	
	    createQMLContentType  -doc $doc `
                -parent $Outcome `
                -a_strContent $a_strContent  
        $i++
    }
    if($a_Answers."choice-f"){
        if($i -eq $nCondition){
    	   $nScore = 1
        }
        else{
           $nScore = 0 
        }
        
        $test = [string]$i
        $test2 = '"' + [string]$i + '"'
        
		$Outcome = createQMLOutcomeType  -doc $doc `
                -parent $parent `
                -a_strId $test `
                -a_nScore $nScore
	
	    createQMLConditionType  -doc $doc `
                -parent $Outcome `
                -a_strCondition $test2  
  	                       	
	    createQMLContentType  -doc $doc `
                -parent $Outcome `
                -a_strContent $a_strContent  
        $i++
    }               
    
}


#
#.SYNOPSIS
#
#
#.DESCRIPTION
#    
#
#----------------------------------------
function createQMLChoice{
    param($doc,$parent, $a_strId, $a_strContent)	            
    $cId = 0
	if($a_strContent."choice-a"){
		$Choice = createQMLChoiceType -doc $doc `
	                -parent $parent `
    	            -a_strId $cId
    
    	            
        if($a_strContent."choice-a".GetType().Name -eq "String"){
            $strContent = $a_strContent."choice-a"
        }
        else{
            $strContent = $a_strContent."choice-a".innerXml
        }
        
		createQMLContentType  -doc $doc `
                -parent $Choice `
                -a_strContent $strContent
                
        $cId++
	}
	
	if($a_strContent."choice-b"){
		$Choice = createQMLChoiceType -doc $doc `
	                -parent $parent `
    	            -a_strId $cId
    	
        if($a_strContent."choice-b".GetType().Name -eq "String"){
            $strContent = $a_strContent."choice-b"
        }
        else{
            $strContent = $a_strContent."choice-b".innerXml
        }
        
		createQMLContentType  -doc $doc `
                -parent $Choice `
                -a_strContent $strContent
        $cId++            
	}
	
	if($a_strContent."choice-c"){
		$Choice = createQMLChoiceType -doc $doc `
	                -parent $parent `
    	            -a_strId $cId
    	
        if($a_strContent."choice-c".GetType().Name -eq "String"){
            $strContent = $a_strContent."choice-c"
        }
        else{
            $strContent = $a_strContent."choice-c".innerXml
        }
        
		createQMLContentType  -doc $doc `
                -parent $Choice `
                -a_strContent $strContent
                
        $cId++        
	}
	
	if($a_strContent."choice-d"){
		$Choice = createQMLChoiceType -doc $doc `
	                -parent $parent `
    	            -a_strId $cId
        
        if($a_strContent."choice-d".GetType().Name -eq "String"){
            $strContent = $a_strContent."choice-d"
        }
        else{
            $strContent = $a_strContent."choice-d".innerXml
        }
        
		createQMLContentType  -doc $doc `
                -parent $Choice `
                -a_strContent $strContent
        $cId++        
	}
    
	if($a_strContent."choice-e"){
		$Choice = createQMLChoiceType -doc $doc `
	                -parent $parent `
    	            -a_strId $cId
    	
        if($a_strContent."choice-e".GetType().Name -eq "String"){
            $strContent = $a_strContent."choice-e"
        }
        else{
            $strContent = $a_strContent."choice-e".innerXml
        }
        
		createQMLContentType  -doc $doc `
                -parent $Choice `
                -a_strContent $strContent
                
        $cId++        
	}
    
	if($a_strContent."choice-f"){
		$Choice = createQMLChoiceType -doc $doc `
	                -parent $parent `
    	            -a_strId $cId
    	
        if($a_strContent."choice-f".GetType().Name -eq "String"){
            $strContent = $a_strContent."choice-f"
        }
        else{
            $strContent = $a_strContent."choice-f".innerXml
        }
        
		createQMLContentType  -doc $doc `
                -parent $Choice `
                -a_strContent $strContent
                
        $cId++        
	}                
    $gCounter = $cId               		
}

#
#.SYNOPSIS
#
#
#.DESCRIPTION
#    
#
#----------------------------------------
function createQMLAnswer{
    param($doc,$parent, $a_strQuestionType, $a_strId, $a_strContent)	                   

	$Answer = createQMLAnswerType -doc $doc `
                -parent $parent `
                -a_strQuestionType $a_strQuestionType 

	createQMLChoice -doc $doc `
	            -parent $Answer `
	            -a_strId $a_strId `
	            -a_strContent $a_strContent	            
}


#
#.SYNOPSIS
#
#
#.DESCRIPTION
#    
#
#----------------------------------------
function createQMLQuestion{
    param($doc,$parent, 
    		$qId, $qContent, $qType, $qDescription,
    		$aId, $aContent,
    		$oId, $oCondition, $oContent
    		)	       	            

    $Question = createQMLQuestionType  -doc $doc `
                -parent $parent `
                -a_strId $qId `
                -a_strDescription $qDescription
                
	createQMLContentType  -doc $doc `
                -parent $Question `
                -a_strContent $qContent                

    createQMLAnswer -doc $doc `
                -parent $Question `
                -a_strQuestionType $qType `
                -a_strId	$aId `
                -a_strContent $aContent
                
	createQMLOutcome  -doc $doc `
                -parent $Question `
                -a_strId $oId `
                -a_strCondition $oCondition `
                -a_strContent $oContent `
                -a_Answers $aContent               
                
                   		
}

#
#.SYNOPSIS
#
#
#.DESCRIPTION
#    
#
#----------------------------------------
function main{
	$doc = New-Object System.Xml.XmlDocument	  
	$root=$doc.CreateElement("QML")
	[void] $doc.AppendChild($root)
	
	[System.Xml.XmlDocument] $xd = new-object System.Xml.XmlDocument
	$file = resolve-path($inputFile)
	$xd.load($file)
	$nodelist = $xd.selectnodes("/examview/question") # XPath is case sensitive
	
	foreach ($question in $nodelist) {
        if($startId){
            $Id = $startId
            $startId++
            }
        else{
            $Id = [int] $question."question-id"
        }
        $length = 16 - $prefix.length
		$qDesc = "$prefix{0:D$length}" -f $Id
        
        $qId = "{0:D16}" -f $Id
        
        if($question.text.GetType().Name -eq "String"){
            $qContent = $question.text
        }
        else{
            $qContent = $question.text.innerXml
        }
		
        #$qContent = $question.text.innerXml
        #$qContent = $question.text
		$qType = $question.type
		$aContent = $question.choices
	    $oCondition = $question.answer
	    $oContent = "..."
		
		
		createQMLQuestion -doc $doc -parent $root `
						-qId $qId `
						-qContent $qContent `
						-qType $qType.ToUpper() `
						-qDescription $qDesc `
						-aId $Id `
						-aContent $aContent `
	    				-oId $Id `
	    				-oCondition $oCondition `
	    				-oContent $oContent
    				
	}    				
	
	$doc.Save($outputFile)
	#    s
}

main